<?php
/**
 * 	app后台首页
 *  =============================
 * 	后台父类
 * 	=============================
 *	Author: 褚天恩
 *  Date: 2017-10-09
 * @author Administrator
 *
 */
namespace app\walletyhbbadmin\controller;
use Think\Db;
use app\admin\controller\Finance;
require_once (APP_PATH .'api/FinanceApi.php');
require_once (APP_PATH .'api/RewardApi.php');
class Index extends AdminBase  {
	public $level_arr;
 	public function _initialize(){
        parent::_initialize();
        
    }
    public function mmm(){
//     	$aa = strtotime("20180706 00:00:00");
    	echo '121231233';
//     	dump($aa);
//     	die();
    	$id = 58927;
    	$where['finance_id'] = array('gt',$id);
    	//$where['finance_type'] = 7;
    	$finance = Db::name('App_finance_copy')->where($where)->select();
    	dump($finance);
		
    	
    }
    public function qqqqq(){
    	$user = Db::name('Users')->where(array('loginName'=>'啊兵'))->find();
    	$member = Db::name('App_member_relation')->where(array('member_id'=>$user['userId']))->find();
    	//等级表
    	$level_arr = Db::name('App_level')->select();
    	$level = $member['level']?$member['level']:0;
    	if($level == 0){
//    		continue;
    	}
    	$class = $level_arr[$level];
    	dump($class);
    	$all_money = $this->getAllCengMember($member, $class['dongtai_ceng_num']);
    	dump($all_money);
    	$jiang = $all_money * $class['dongtai_bili'] * 0.01;
    	
    	$jiang = min($jiang,$class['dongtai_top_day']);
    	
    	dump($jiang);
    }
    
    
    public function dongtai(){
    	
    	//$member = $this->getDongTaiMember();
    	$where['a.status'] = array('neq',0);
    	$where['a.level'] = array('neq',0);
    	$member = Db::table('wst_app_member_relation')
    						->field('a.member_id,a.zhuceid,a.level,a.lft,a.rgt,a.ceng,a.status,b.member_id,b.account_type_2')
							->alias('a')
							->join('wst_app_member_account b','a.member_id = b.member_id')
							->where($where)
							->select();
//     	dump($member);
//     	die();
    	if(empty($member)){
    		return false;
    	}
    	//等级表
    		$level_arr = Db::name('App_level')->select();
    	foreach($member as $k=>$v){
    		$level = $member[$k]['level']?$member[$k]['level']:0;
    		if($level == 0){
    			continue;
    		}
    		$class = $level_arr[$level];
    		$all_money = 0;
    		$all_money = $this->getAllCengMember($member[$k], $class['dongtai_ceng_num']);
    		$jiang = $all_money * $class['dongtai_bili'] * 0.01;
    
    		$jiang = min($jiang,$class['dongtai_top_day']);
    		dump($jiang);
//     		if($jiang > 0){
//     			$res[] = $this->addUserMoney($member[$k]['member_id'],$jiang,3);
//     		}
    		unset($all_money);
    		unset($jiang);
    		//修改is_release字段
    		//$res[] = $this->updatedong($v['member_id']);
    	}
    	return true;
    }
    /**
     * 给用户钱包加钱
     *
     * @param unknown $userId
     * @param unknown $money
     */
    public function addUserMoney($member_id,$money,$type){
    	$where['finance_id'] = array('gt',58927);
    	$where['finance_type'] = 7;
    	$where['member_id'] = $member_id;
    	$finance = Db::name('App_finance')->where($where)->find();
    	if(!empty($finance)){
    		return true;
    	}
    	//根据类型获得对应参数
    	$arr['content'] = '动态奖励';
    	$arr['account_type'] = 4;
    	$arr['account_type_name'] = '动态积分';
    	$arr['finance_type'] = 7;
    	$r[] = Db::name('App_member_account')->where(array('member_id'=>$member_id))->setInc('account_type_4',$money);
    	$finance = new \FinanceApi();
    	//添加日志
    	$r[] = $finance->addFinance($member_id,$arr['finance_type'],$arr['content'].$arr['account_type_name'],1,$money,$arr['account_type']);
    	return $r;
    }
    /**v
     * 获得整层用户
    * @param unknown $member_id
    * @param unknown $ceng
    */
    public function getAllCengMember($member,$ceng){
    	
    	$now_ceng = $member['ceng'] + $ceng;
    	$where['a.lft'] = array('gt',$member['lft']);
    	$where['a.rgt'] = array('lt',$member['rgt']);
    	$where['a.ceng'] = array('elt',$now_ceng);
    	$list = Db::table('wst_app_member_relation')
    				->field('a.member_id,a.zhuceid,a.level,a.lft,a.rgt,a.ceng,a.status,b.member_id,b.account_type_2')
					->alias('a')
					->join('wst_app_member_account b','a.member_id = b.member_id')
					->where($where)
					->select();
    	//dump($list);
    			//var_dump($list);
    	$all_money = 0;
    	foreach($list as $k=>$v){
    		$all_money += $list[$k]['account_type_2'];
    	}
    	return $all_money;
    }
    
    public function ppp(){
    	$where['member_id'] = array('neq',1);
    	    	$data['is_dong'] = 0;
//     	    	$data['level'] = 0;
    	    	$data['is_release'] = 0;
    	    	$res = Db::name('App_member_relation')->where($where)->update($data);
    }
    
    
    
    public function nnn(){
    	$level_arr = $this->level_arr;
//     	$where['member_id'] = array('neq',1);
//     	$data['is_dong'] = 0;
//     	$data['level'] = 0;
//     	$data['is_release'] = 0;
//     	$res = Db::name('App_member_relation')->where($where)->update($data);
    	$loginName[] = '丽888';
    	$loginName[] = '15941081042';
    	$loginName[] = '13866762057';
    	$loginName[] = '13886134817';
    	$loginName[] = '18982797195';
    	$loginName[] = '18765101282';
    	$loginName[] = '18337754365';
    	$loginName[] = '15855875609';
    	$loginName[] = 'xmg123';
    	$loginName[] = '啊兵';
    	$loginName[] = '517648zjy';
    	$where['loginName'] = array('in',$loginName);
    	$user = Db::name('Users')->where($where)->select();
    	foreach($user as $k=>$v){
    		$arr[] = $user[$k]['userId'];
    	}
    	$where_member['member_id'] = array('in',$arr);
    	$member = Db::name('App_member_relation')->where($where_member)->select();
    	
    	foreach($member as $m=>$n){
    		dump($member[$m]);
    		$level = $member[$m]['level'];
    		$class = $level_arr[$level];
    		dump($class);
    		for($i = $class['dongtai_ceng_num']; $i > 0;$i--){
    			dump($class['dongtai_ceng_num']);
   				$all_money += $this->getAllCengMember($member[$k], $i);
   			}
   			dump($all_money);
   			$jiang = $all_money * $class['dongtai_bili'] * 0.01;
   			$jiang = min($jiang,$class['dongtai_top_day']);
   			unset($all_money);
   			unset($jiang);
    	}
    	
    }
    public function getMemberLevel($member_id,$account){
    	
    	$where['money_min'] = array('lt',$account);
    	$where['money_max'] = array('gt',$account);
    	$level = Db::name('App_level')->where($where)->find(); 
    	dump($level);
    	$id = $level['id'];
    	$level_arr = Db::name('App_level')->select();
    	
    	//六星会员
    	if($level['id'] == 7){
    	//判断满足不满足六星会员其他的条件
    	$num = $this->getDownNumFiveStar($member_id,$level_arr);
    		//     		$level_six =  $this->database->select("app_level","*", [
    		//     				"app_level.id" =>7
    		//     				])[0];
    		$level_six = $level_arr[5];
    		if($num >= $level_six['member_num']){
    		$id = 7;
    	}else{
    	$level['id'] = 6;
    	}
    	}
    	//五星会员
    	if($level['id'] == 6){
    	//判断满足不满足五星会员其他的条件
    	$num = $this->getDownNumFourStar($member_id,$level_arr);
    	// 			$level_five =  $this->database->select("app_level","*", [
    	// 					"app_level.id" => 6
    	// 					])[0];
    	$level_five = $level_arr[5];
    	if($num >= $level_five['member_num']){
    	//5
    	$id = 6;
    	}else{
    	//4
    	$id = 5;
    	}
    	}
    	 
    	//     	$level =  $this->database->select("app_level","*", [
    			// 	    			"app_level.id" => $id
    			//     		])[0];
    	$level = $level_arr[$id-1];
    	$data['level'] = $id-1;
    	$r = Db::name('App_member_relation')->where(array('member_id'=>$member_id))->update($data);
    	dump($level);
    	
    	
    	
    	
    	return $level;
    }
    
    /**
     * 会等下线等级四星会员以上人数
     */
    public function getDownNumFourStar($member_id,$level_arr){
    	
    	$four_level = $level_arr[4];
    	$where['w.account_type_2'] = array('lt',$four_level['money_min']);
    	$where['a.zhuceid'] = $member_id;
    	$member = Db::table('wst_app_member_relation')
					->alias('a')
					->join('wst_app_member_account w','a.member_id = w.member_id')
					->where($where)
					->select();
    	
    	return count($member);
    }
    /**
     * 获得下线等级五星会员的人数
     * @param unknown $member_id
     * @return number
     */
    public function getDownNumFiveStar($member_id,$level_arr){
    	$five_level = $level_arr[5];
    	$where['w.account_type_2'] = array('lt',$five_level['money_min']);
    	$where['a.zhuceid'] = $member_id;
    	$member = Db::table('wst_app_member_relation')
			    	->alias('a')
			    	->join('wst_app_member_account w','a.member_id = w.member_id')
			    	->where($where)
			    	->select();
    	
    	foreach($member as $k=>$v){
    		$this_member_down_four = $this->getDownNumFourStar($member[$k]['member_id'],$level_arr);
    		if($this_member_down_four < $five_level['member_num']){
    			unset($member[$k]);
    		}
    	}
    	return count($member);
    }
    
    
    
    
    
    
    
    
    
    
    public function rebuild_tree($member_id, $left){
    	$right = $left + 1;
    	$res = Db::name('App_member_relation')->where(array('zhuceid' => $member_id))->order('member_id asc')->select();
    	foreach ($res as $k => $v){
    		$right = $this->rebuild_tree($v['member_id'], $right);
    	}
    	$data['lft'] = $left;
    	$data['rgt'] = $right;
    	Db::name('App_member_relation')->where(array('member_id'=>$member_id))->update($data);
    	return ($right + 1);
    }
    
    public function go(){
    	$this->rebuild_tree(1,1);
    }
    
    
    public function aaa(){
    	$member_list = Db::name('App_member_relation')->select();
    	foreach($member_list as $k=>$v){
    		//团队人数
    		$where['lft'] = array('gt',$member_list[$k]['lft']);
    		$where['lft'] = array('lt',$member_list[$k]['rgt']);
    		$teamNum[$k] = ($member_list[$k]['rgt']-$member_list[$k]['lft']-1)/2;
    		echo $member_list[$k]['member_id'].'团队人数'.$teamNum[$k];
    		echo "<br>";
    	}
    }
    public function bb(){
    	$where['member_id'] = array('neq',1);
    	$member = Db::name('App_member_relation')->where($where)->order('member_id asc')->select();
    	foreach($member as $k=>$v){
    		$zhuce = Db::name('App_member_relation')->where(array('member_id'=>$member[$k]['zhuceid']))->find();
    		$data['ceng'] = $zhuce['ceng']+1;
    		$res[] = Db::name('App_member_relation')->where(array('member_id'=>$member[$k]['member_id']))->update($data);
    		dump($member[$k]['member_id']);
    		echo "<br>";
    	}
    
    }
    public function ccc(){
    	$where['finance_type'] = 1;
    	$list = Db::name('App_finance')->where($where)->select();
    	foreach($list as $k=>$v){
    		$re[] = Db::name('App_member_account')->where(array('member_id'=>$list[$k]['member_id']))->setDec('account_type_2',$list[$k]['money']);
    		$re[] = Db::name('App_finance')->where(array('finance_id'=>$list[$k]['finance_id']))->delete();
    	}
    	$where1['finance_type'] = 6;
    	$list1 = Db::name('App_finance')->where($where1)->select();
    	foreach($list1 as $m=>$n){
    		$re[] = Db::name('App_member_account')->where(array('member_id'=>$list1[$m]['member_id']))->setDec('account_type_4',$list1[$m]['money']);
    		$re[] = Db::name('App_finance')->where(array('finance_id'=>$list1[$m]['finance_id']))->delete();
    	}
    }
    
    
    
	/**
	 * 后台首页
	 * @return Ambigous <\think\mixed, string>
	 */
    public function index(){
    	
		
        return $this->fetch();
    }
    public function getImformation($time){
    	//查询的日期
    	$riqi = date('Y-m-d',$time[0]);
    	//查询的数据表
    	//$db = 'App_finance_'.date('Y_m',$time[0]);
    	//Y-m-d 格式时间
    	$ymd = $this->getYmdByTime($time);
    	//统计riqi这天的数据
    	$list['time'] = $riqi;
    	//收入充值TODO
    	// 			$list['get']
    	//提现TODO
    	// 			$list['give']
    	//奖励
    	$where1['add_time'] = array('between',$time);
    	$where1['finance_type'] = array('in',$this->getTypeArr());
    	$account_type = Db::name('App_account_type')->select();
    	foreach($account_type as $m=>$n){
    		$where1['account_type'] = $account_type[$m]['id'];
    		$list['jiang_account_type_'.$account_type[$m]['id']] =  Db::name('App_finance')->where($where1)->sum('money');
    		$list['jiang_account_type_'.$account_type[$m]['id']] = $list['jiang_account_type_'.$account_type[$m]['id']]>0?$list['jiang_account_type_'.$account_type[$m]['id']]:'0.00';
    	}
    	//新增会员
    	$map2['createTime'] = array('between',$ymd);
    	$list['num'] = M('Users')->where($map2)->count();
    	
    	return $list;
    }
    public function getYmdByTime($time){
    	$date[] = date('Y-m-d H:i:s',$time[0]);
    	$date[] = date('Y-m-d H:i:s',$time[1]);
    	return $date;
    }
    //获得奖励财务日志数组
    public function getTypeArr(){
    	//推荐奖励
    	$arr[] = 8;
    	//注册奖励
    	$arr[] = 9;
    	//对碰奖励
    	$arr[] = 10;
    	//管理奖励
    	$arr[] = 11;
    	//报单中心管理奖
    	$arr[] = 12;
    	return $arr;
    }
    
    /**
     * 今日零点到24点时间段
     * 数组形式返回
     */
    public function toDayTime(){
    	$start = strtotime(date('Y-m-d',time()));
    	$time[] = $start;
    	$time[] = $start+84600;
    	return $time;
    }
	/**
	*	测试
	*/
     public function show(){
		 $where['old_yeji'] = array('gt',0);
		 $where['zhitui_num'] = 0;
		 $list = Db::name('App_member_relation')->where($where)->select();
		 dump($list);
		 die();
		 
		 
		 
		 
		 $idcard_array = array();
		foreach($list as $k=>$v){
			$idcard = $list[$k]['idcard'];
			$res = Db::name('App_member_relation')->where(array('idcard'=>$idcard))->count();
			if($res > 1){
				if(!in_array($list[$k]['idcard'],$idcard_array)){
					$idcard_array[] = $idcard;
					$arr[]['idcard'] = $idcard;
				}
			}
		}
		
	 }
	 /**
	  * 结算对碰
	  */
	 public function duipengOver(){
	 	$where['duipeng_left'] = array('gt',0);
	 	$where['duipeng_righ'] = array('gt',0);
	 	$where['status'] = array('neq',0);
	 	$member_relation = Db::name('App_member_relation')->where($where)->select();
	 	$reward = new \RewardApi();
	 	foreach($member_relation as $k=>$v){
	 		$res[] = $reward->duipeng($member_relation[$k]['member_id']); 
	 	}
	 	if($res){
	 		$this->success('成功');
	 	}else{
	 		$this->error('失败');
	 	}
	 	
	 }
	 /**
	  * 清空数据
	  */
	 public function wipe(){
	 	$where1['member_id'] = array('neq',1);
	 	$where2['userId'] = array('neq',1);
	 	$r[] = Db::name('Users')->where($where2)->delete();
	 	$r[] = Db::name('App_member_relation')->where($where1)->delete();
	 	
	 	$r[] = Db::name('Line')->where($where1)->delete();
	 	$data['yeji_righ'] = 0;
	 	$data['yeji_left'] = 0;
	 	$data['duipeng_righ'] = 0;
	 	$data['duipeng_left'] = 0;
	 	$data['righ_num'] = 0;
	 	$data['left_num'] = 0;
	 	$data['left_surplus'] = 0;
	 	$data['righ_surplus'] = 0;
	 	$data['zhitui_num'] = 0;
	 	$r[] = Db::name('App_member_relation')->where(array('member_id'=>1))->update($data);
	 	$where3['member_id'] = array('neq',0);
	 	$r[] = Db::name('App_fwipeinance')->where($where3)->delete();
	 	$r[] = Db::name('withdraw')->where($where3)->delete();
	 	$r[] = Db::name('App_chongzhi')->where($where3)->delete();
	 	$r[] = Db::name('App_transfer')->where($where3)->delete();
	 	$r[] = Db::name('Goods_user')->where($where3)->delete();
	 	$r[] = Db::name('App_layer')->where($where3)->delete();
	 	$r[] = Db::name('App_member_account')->where($where3)->delete();
	 	$one['member_id'] = 1;
	 	$one['account_type_2'] = 99999999;
	 	$r[] = Db::name('App_member_account')->insert($one);
	 	$config['now_new_yeji'] = 0;
	 	$config['now_new_jili_yeji'] = 0;
	 	foreach ($config as $k=>$v){
	 		$rs[] = Db::name('app_config')->where("wst_app_config.key='{$k}'")->setField('value',$v);
	 	}
	 	
	 	$where4['id'] = array('neq',0);
	 	$r[] = Db::name('App_statistics')->where($where4)->delete();
	 	if($r){
	 		$this->success('成功');
	 	}else{
	 		$this->error('失败');
	 	}
	 	
	 }
	 /**
	  * 释放股东分红
	  */
	 public function releaseGuDong(){
	 	$now_new_yeji = input('now_new_yeji');
	 	if(!$now_new_yeji){
	 		$this->error('请填写需要释放的当月新增业绩');
	 	}
	 	$where['status'] = 1;
	 	$where['level'] = 5;
	 	//当前所有的股东的数量
	 	$count = Db::name('App_member_relation')->where($where)->count();
	 	if(empty($count)){
	 		$this->error('暂无股东');
	 	}
	 	$reward = new \RewardApi();
	 	$re = $reward->gudong($now_new_yeji);
	 	if($re){
	 		$this->success('释放成功');
	 	}else{
	 		$this->error('释放完成');
	 	}
	 	
	 }
	 /**
	  * 释放激励分红
	  */
	 public function releaseJiLi(){
	 	$now_new_jili_yeji = input('now_new_jili_yeji');
	 	if(!$now_new_jili_yeji){
	 		$this->error('请填写需要释放的当月新增业绩');
	 	}
	 	$reward = new \RewardApi();
	 	$re = $reward->jili($now_new_jili_yeji);
	 	if($re){
	 		$this->success('释放成功');
	 	}else{
	 		$this->error('释放完成');
	 	}
	 }
	 
	 
	 public function bbbbb(){
	 	$where['level'] = array('neq',5);
	 	$list = Db::name('App_member_relation')->where($where)->order('id desc')->select();
	 	foreach($list as $k=>$v){
	 		$data['yeji_righ'] = 0;
	 		$data['yeji_left'] = 0;
	 		$data['tream_num'] = 0;
	 		$data['yeji'] = 0;
	 		$res[] = Db::name('App_member_relation')->where(array('member_id'=>$list[$k]['member_id']))->update($data);
	 	}
	 	dump($res);
	 }
	 
	 
	 public function ccccc(){
	 	$where['level'] = array('neq',5);
	 	$where['status'] = 1;
	 	$list = Db::name('App_member_relation')->where($where)->order('id desc')->select();
	 	foreach($list as $k=>$v){
	 		$money = $this->getLevelMoney($list[$k]['level']);
	 		$res[] = $this->addYeJi($list[$k]['member_id'], $money);
	 		unset($money);
	 	}
	 	dump($res);
	 }
	 public function getLevelMoney($level){
	 	$arr[1] = 700;
	 	$arr[2] = 2100;
	 	$arr[3] = 7000;
	 	$arr[4] = 21000;
	 	return $arr[$level];
	 }
	 
	 public function addYeJi($member_id,$money){
	 	$reward = new \RewardApi();
	 	$list = $reward->getUpUserByjiedianId($member_id,9999);
	 	for($i=1;$i<count($list);$i++){
	 		if($list[$i-1]['quyu'] == 'righ'){
	 			$data['yeji_righ'] = $list[$i]['yeji_righ']+$money;
	 		}else{
	 			$data['yeji_left'] = $list[$i]['yeji_left']+$money;
	 		}
	 		//为上线加业绩
	 		$data['yeji'] = $list[$i]['yeji']+$money;
	 		//为上线加团队人数
	 		$data['tream_num'] = $list[$i]['tream_num']+1;
	 		$res[] = M('App_member_relation')->where('member_id='.$list[$i]['member_id'])->update($data);
	 		unset($data);
	 	}
	 	return $res;
	 }


    //支付二维码设置页面
    public function CodeManager(){
        $config_z = Db::name('app_config')->where('key','zfb_code')->find();
        $config_w = Db::name('app_config')->where('key','wx_code')->find();
        $this->assign('list_z',$config_z);
        $this->assign('list_w',$config_w);
        return $this->fetch('code');
    }
	 
    //支付宝二维码上传
    public function zfb_add(){
        $pic = request()->file('zfb');
        if(!empty($pic)){
            $info = $pic->move ( ROOT_PATH . 'public' . DS . 'uploads' );
            $header_img = '/public/uploads/' . str_replace('\\', '/', $info->getsaveName ());
            $re['value'] = $header_img;
        }else{
            $this->error('请选择上传图片');
        }
        $rs=Db::name('app_config')->where("key",'zfb_code')->update($re);
        if($rs){
            $this->success('配置修改成功');
        }else{
            $this->error('配置修改失败');
        }
    }



    //支付宝二维码上传
    public function wx_add(){
        $pic = request()->file('wx');
        if(!empty($pic)){
            $info = $pic->move ( ROOT_PATH . 'public' . DS . 'uploads' );
            $header_img = '/public/uploads/' . str_replace('\\', '/', $info->getsaveName ());
            $re['value'] = $header_img;
        }else{
            $this->error('请选择上传图片');
        }
        $rs=Db::name('app_config')->where("key",'wx_code')->update($re);
        if($rs){
            $this->success('配置修改成功');
        }else{
            $this->error('配置修改失败');
        }
    }
	 
}